﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;

namespace Services {
    public class WebDataContext : DbContext {
        public delegate void OnModelCreatingEventHandler(ref DbModelBuilder modelBuilder);
        event OnModelCreatingEventHandler _OnModelCreatingEvent;

        public event OnModelCreatingEventHandler OnModelCreatingEvent {
            add {
                _OnModelCreatingEvent += value;
            }
            remove {
                _OnModelCreatingEvent -= value;
            }
        }

        public WebDataContext()
            : base("name=WebDataDb") {
            this.Configuration.LazyLoadingEnabled = false;
            Database.SetInitializer<WebDataContext>(null);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            if (_OnModelCreatingEvent != null) {
                _OnModelCreatingEvent(ref modelBuilder);
            }
            //modelBuilder.Entity<WEBDATA_PM25Info>().ToTable("webdata_pm25data2016");

            base.OnModelCreating(modelBuilder);
        }
    }

    //public class WebDataEntityTypeConfiguration<T> : EntityTypeConfiguration<T> {
    //    public WebDataEntityTypeConfiguration()
    //        : base() {
    //        base.ToTable("webdata_pm25data2016");
    //    }
    //}
}
